package com.lofter.heras.hash;

import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import com.lofter.heras.ui.State;

/**
 * progressbar handler
 * 
 * @author heras
 * 
 */
public class ProgressBarHandler implements Handler {

	private JProgressBar bar;

	public ProgressBarHandler(JProgressBar bar) {
		this.bar = bar;
	}

	@Override
	public void update(long current, long total) {
		if (bar != null) {
			final long v1 = current;
			final long v2 = total;
			double pect = (total == 0) ? 0 : (double) current / total;
			final int v = (int) (pect * 100);
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {

					String pectange = "{0}/{1}({3}%)"
							.replace("{0}", String.valueOf(v1))
							.replace("{1}", String.valueOf(v2))
							.replace("{3}", String.valueOf(v));
					bar.setString(pectange);
					bar.setValue(v);
				}
			});
		}
	}

	@Override
	public void complete() {
		State.running = false;
	}

}
